package com.zttek.videoShareProject.modules.web;

import com.zttek.videoShareProject.common.json.Result;
import com.zttek.videoShareProject.common.tree.BuildTree;
import com.zttek.videoShareProject.common.tree.MenuDto;
import com.zttek.videoShareProject.common.tree.Tree;
import com.zttek.videoShareProject.modules.model.User;
import com.zttek.videoShareProject.modules.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZYZ on 2018/7/7
 */
@Controller
@RequestMapping("/main")
public class MenuController {
    @Autowired
    private IUserService iUserService;

    @RequestMapping
    public ModelAndView initIndex() {
        return new ModelAndView("/main/index");
    }
    @RequestMapping("/menu")
    @ResponseBody
    public Result initMenu() throws Exception {
        System.out.println("aa");
        ModelAndView modelAndView=new ModelAndView("/main/index");
        List<Tree<MenuDto>> trees = new ArrayList<Tree<MenuDto>>();
        List<MenuDto> menuDtoList = new ArrayList<MenuDto>();
        User user=iUserService.selectUserById(Long.parseLong("2"));
        Result result=new Result();



        menuDtoList.add(new MenuDto(1L,"主菜单",1L,""));
        menuDtoList.add(new MenuDto(2L,"权限系统",1L,""));
        menuDtoList.add(new MenuDto(3L,"视频管理",1L,""));
        menuDtoList.add(new MenuDto(4L,"用户管理",2L,"/vss/user/userList"));
        menuDtoList.add(new MenuDto(9L,"视频发布管理",3L,"/vss/video/videoList"));
        menuDtoList.add(new MenuDto(10L,"视频举报管理",3L,"/vss/video/videoReported"));
        menuDtoList.add(new MenuDto(11L,"敏感词管理",3L,"/vss/keyword/keyWord"));
        for (MenuDto test : menuDtoList) {
            Tree<MenuDto> tree = new Tree<MenuDto>();
            tree.setId(test.getId().toString());
            tree.setParentId(test.getParent_id().toString());
            tree.setUrl(test.getUrl());
            tree.setText(test.getMenuNmae());
            trees.add(tree);
        }
        List<Tree<MenuDto>> children= BuildTree.build(trees);
        result.setSuccess(true);
        result.add("children",children);

        return result;
    }
    @RequestMapping("/welcome")
    public ModelAndView welcome()throws Exception{
        User user=iUserService.selectUserById(Long.parseLong("2"));
        if(StringUtils.isBlank(user.getAccount())){
            return new ModelAndView("/main/welcome");
        }
        return new ModelAndView("/main/welcome").addObject("account",user.getAccount());
    }

}
